package com.aj.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import android.graphics.Bitmap;
import android.os.Environment;

public class FileHelper {
	public static boolean isFileExist(String path) {
		if ( path == null )
			return false;
		else if ( path.length() == 0 )
			return false;
		else {
			File file = new File(path);
			if (file.exists())
				return true;
			else
				return false;
		}
	}
	
	/**
	 * 递归删除文件和文件夹
	 * 
	 * @param file
	 *            要删除的根目录
	 */
	public static void RecursionDeleteFile(File file) {
		if (file.isFile()) {
			file.delete();
			return;
		}
		if (file.isDirectory()) {
			File[] childFile = file.listFiles();
			if (childFile == null || childFile.length == 0) {
				file.delete();
				return;
			}
			for (File f : childFile) {
				RecursionDeleteFile(f);
			}
			file.delete();
		}
	}

	public static String ReadFile(String path) {
		File file = new File(path);
		BufferedReader reader = null;
		String laststr = "";
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				laststr = laststr + tempString;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return laststr;
	}

	public static void writeAllText(String fileName, String text) throws FileNotFoundException {
		File file = new File(fileName);
		if (!file.exists()) {
			RandomAccessFile mm = null;
			try {
				mm = new RandomAccessFile(file, "rw");
				mm.writeBytes("");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (mm != null) {
					try {
						mm.close();
					} catch (IOException e2) {
						// TODO 自动生成 catch 块
						e2.printStackTrace();
					}
				}
			}
		}
		PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
		pw.print(text);
		pw.flush();
		pw.close();
	}

	public static String saveBitmap(String path, String name, Bitmap mBitmap) {
		String sdStatus = Environment.getExternalStorageState();
		if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
			return null;
		}
		FileOutputStream b = null;
		File file = new File(path);
		file.mkdirs();// 创建文件夹
		String fileName = path + name + ".jpg";

		try {
			b = new FileOutputStream(fileName);
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				b.flush();
				b.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return fileName;
	}

	public static boolean isEffective(String path, String name) {
		File file = new File(path);
		if (file.exists() && (name.length() > 3) && (file.length() > 0))
			return true;
		else
			return false;
	}
}
